package modeleditor.editor;

import ganttchart.Ganttchart;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JPanel;

import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.IntervalCategoryDataset;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;
import org.jfree.data.time.SimpleTimePeriod;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 * A simple demonstration application showing how to create a Gantt chart with 
 * multiple bars per task. 
 */
public class drawganttchart {

	private static Ganttchart ganttchart;
	
    public drawganttchart(Ganttchart ganttchart) {

        this.ganttchart = ganttchart;
        //IntervalCategoryDataset dataset = createDataset();
        //JFreeChart chart = createChart(dataset);

    }
    
    /**
     * Creates a sample chart.
     * 
     * @param dataset  the dataset.
     * 
     * @return A sample chart.
     */
    public static JFreeChart createChart(IntervalCategoryDataset dataset) {
        JFreeChart chart = ChartFactory.createGanttChart(
            ganttchart.getChartname(),  // chart title
            ganttchart.getYname(),              // domain axis label
            ganttchart.getXname(),              // range axis label
            dataset,             // data
            true,                // include legend
            true,                // tooltips
            false                // urls
        );
        
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.getDomainAxis().setMaximumCategoryLabelWidthRatio(10.0f);
        CategoryItemRenderer renderer = plot.getRenderer();
        renderer.setSeriesPaint(0, Color.blue);
        DateAxis da = (DateAxis)plot.getRangeAxis(0);
        da.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd"));
        return chart;
    }
    
    /**
     * Creates a sample dataset for a Gantt chart, using sub-tasks.  In 
     * general, you won't hard-code the dataset in this way - it's done here so
     * that the demo is self-contained.
     *
     * @return The dataset.
     */
    public static IntervalCategoryDataset createDataset() {

        TaskSeriesCollection collection = new TaskSeriesCollection();
    	for(int i=0; i<ganttchart.getTaskserieses().size(); i++){
    		if(ganttchart.getTaskserieses().get(i).getType().getName().equals("Scheduled")){
    			TaskSeries s1 = new TaskSeries("Scheduled");
    	        collection.add(s1);
    			for(int j=0; j<ganttchart.getTaskserieses().get(i).getTasks().size(); j++){
    				s1.add(new Task(ganttchart.getTaskserieses().get(i).getTasks().get(j).getTname(), tansferdate(ganttchart.getTaskserieses().get(i).getTasks().get(j).getStarttime(),ganttchart.getTaskserieses().get(i).getTasks().get(j).getEndtime())));
    			}
    		} else if(ganttchart.getTaskserieses().get(i).getType().getName().equals("Actual")){
    			TaskSeries s2 = new TaskSeries("Actual");
    	        collection.add(s2);
    			for(int j=0; j<ganttchart.getTaskserieses().get(i).getTasks().size(); j++){
    				s2.add(new Task(ganttchart.getTaskserieses().get(i).getTasks().get(j).getTname(), tansferdate(ganttchart.getTaskserieses().get(i).getTasks().get(j).getStarttime(),ganttchart.getTaskserieses().get(i).getTasks().get(j).getEndtime())));
    			}
    		}
    	}

        return collection;
    }
    
    private static SimpleTimePeriod tansferdate(String start, String end){
    	String[] mystart = start.split("-");
    	String[] myend = end.split("-");
    	SimpleTimePeriod a = new SimpleTimePeriod(date(Integer.parseInt(mystart[0]),Integer.parseInt(mystart[1])-1,Integer.parseInt(mystart[2])),date(Integer.parseInt(myend[0]),Integer.parseInt(myend[1])-1,Integer.parseInt(myend[2])));
    	return a;
    }
    /**
     * Utility method for creating <code>Date</code> objects.
     *
     * @param day  the date.
     * @param month  the month.
     * @param year  the year.
     *
     * @return A date.
     */
    private static Date date(int day, int month, int year) {

        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day);
        final Date result = calendar.getTime();
        return result;

    }

    /**
     * Creates a panel for the demo (used by SuperDemo.java).
     * 
     * @return A panel.
     */
    public static JPanel createDemoPanel() {
        JFreeChart chart = createChart(createDataset());
        return new ChartPanel(chart);
    }

}
 